package com.cyt.${module}.controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.cyt.activiti.model.dto.ActivitiDTO;
import com.cyt.activiti.model.vo.ActivitiVo;
import com.cyt.activiti.service.ActService;
import com.cyt.base.aspect.BaseLog;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.${module}.model.vo.${entity}Vo;
import com.cyt.${module}.service.${entity}Service;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.Model;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.cyt.sys.controller.SysBaseController;

/**
 * ${tableComment}控制层
 *
 * @author ${author}
 * @date ${.now}
 */
@RestController
public class ${entity}Controller extends SysBaseController {

@Resource
private ${entity}Service ${entity?uncap_first}Service;
@Autowired
private RepositoryService repositoryService;
@Resource
private ActService actService;
/**
 * 统一跳转
 *
 * @param model 数据模型
 * @return Model
 * @author ${author}
 * @date ${.now}
 */
@RequestMapping("/${module}/${entity?uncap_first}/*")
public Model goPage(Model model) {
        return model;
        }

/**
 * 分页查询
 *
 * @param ${entity?uncap_first}Vo 接受参数对象
 * @param page 当前页面
 * @param limit 页面数量
 * @return ReturnResult
 * @author ${author}
 * @date ${.now}
 */
@BaseLog(module = "${moduleLogName}", classz = "${classLogName}", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
@RequestMapping(value = "/${module}/${entity?uncap_first}/page", method = RequestMethod.POST)
public ReturnResult page(${entity}Vo ${entity?uncap_first}Vo, int page, int limit) {
        return ${entity?uncap_first}Service.page(${entity?uncap_first}Vo, page, limit);
        }

/**
 * 跳转添加或编辑界面
 *
 * @param id 主键
 * @return ReturnResult 对象
 * @author ${author}
 * @date ${.now}
 */
@RequestMapping(value = "/${module}/${entity?uncap_first}/getAddOrEdit", method = RequestMethod.GET)
public ModelAndView getAddOrEdit(String id) {
        ModelAndView view = new ModelAndView("/${module}/${entity?uncap_first}/${entity?uncap_first}Edit");
        if (StringUtils.isNotBlank(id)) {
<#if (fieldIsRelate?? && fieldIsRelate !='')>
        view.addObject(${entity?uncap_first}Service.selectById(id));
<#else>
        view.addObject(${entity?uncap_first}Service.selectByPrimaryKey(id));
</#if>
        }
        return view;
        }

/**
 * 跳转流程审批页面
 *
 * @author cai deYong
 * @date 16:07 2019/4/23 0023
 * @param id 请假表单id
 * @return ModelAndView
 **/
@RequestMapping(value = "/activiti/${entity?uncap_first}/getCheck", method = RequestMethod.GET)
public ModelAndView getCheck(String id) {
    ModelAndView view = new ModelAndView("/${module}/${entity?uncap_first}/${entity?uncap_first}Check");
    if (StringUtils.isNotBlank(id)) {
        view.addObject("${entity?uncap_first}Vo",${entity?uncap_first}Service.selectByPrimaryKey(id));
    }
    return view;
}

/**
 * 添加或更新并启动流程
 *
 * @param request 对象
 * @param ${entity?uncap_first}Vo 对象
 * @return ReturnResult 对象
 * @author ${author}
 * @date ${.now}
 */
@BaseLog(module = "${moduleLogName}", classz = "${classLogName}", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
@RequestMapping(value = "/${module}/${entity?uncap_first}/addOrUpdate", method = RequestMethod.POST)
public ReturnResult addOrUpdate(HttpServletRequest request, ${entity}Vo ${entity?uncap_first}Vo,ActivitiVo activitiVo) {
        SysUser sysUser = (SysUser) request.getSession().getAttribute(BaseConstant.SESSION_USER);
        if(sysUser!=null){
            ReturnResult returnResult=${entity?uncap_first}Service.addOrUpdate(${entity?uncap_first}Vo,sysUser);
            ReturnResult returnResultNew=new ReturnResult();
            //若保存成功则启动流程
            if(returnResult.getMsg().equals(BaseConstant.DATABASE_SUCCEED_MSG)){
                //根据key查询是否部署,act_re_procdef
                ProcessDefinition processDefinition= repositoryService.createProcessDefinitionQuery().processDefinitionKey(activitiVo.getKey()).latestVersion().singleResult();
                //已经部署
                if (processDefinition!=null) {
                    if(StringUtils.isEmpty(activitiVo.getTaskId())){
                         activitiVo.setBusinessKey(returnResult.getData().toString());
                         activitiVo.setProcessName("");
                         activitiVo.setStartUserId(sysUser.getId());
                         activitiVo.setProcessDefinitionKey(processDefinition.getKey());
                    }
                    returnResultNew = actService.startTask(activitiVo, sysUser);
                } else {
                    returnResultNew.setCode(ErrorCode.CODE_1);
                    returnResultNew.setMsg("请先部署请假申请模型");
             }
          }else{
                return returnResult;
        }
           return returnResultNew;
    }else{
        return new ReturnResult(ErrorCode.CODE_400, ErrorCode.CODE_400_MSG);
    }
}

/**
 * 查询详情
 *
 * @param activitiDTO 查询信息
 * @return ReturnResult 对象
 * @author ${author}
 * @date ${.now}
 */
@BaseLog(module = "${moduleLogName}", classz = "${classLogName}", methods = "查询详情")
@RequestMapping(value = "/${module}/${entity?uncap_first}/get${entity}Info.do", method = RequestMethod.POST)
public ReturnResult get${entity}Info(HttpServletRequest request, ActivitiDTO activitiDTO) {
        SysUser sysUser = (SysUser) request.getSession().getAttribute(BaseConstant.SESSION_USER);
        ReturnResult returnResult=new ReturnResult();
        if(sysUser!=null){
            returnResult=${entity?uncap_first}Service.get${entity}Info(activitiDTO,sysUser);
        }
        return returnResult;
   }


/**
 * 根据主键id集合软删除
 *
 * @param id 主键id集合，多个以‘，’隔开
 * @return ReturnResult 对象
 * @author ${author}
 * @date ${.now}
 */
@BaseLog(module = "${moduleLogName}", classz = "${classLogName}", methods = "删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
@RequestMapping(value = "/${module}/${entity?uncap_first}/removeByIds", method = RequestMethod.GET)
public ReturnResult removeByIds(HttpServletRequest request, String id) {
        return ${entity?uncap_first}Service.removeByIds(id, acceptCurrentUser(request));
    }
}